local skel = fk.CreateSkill {
  name = "emo__yuanjie",
}

Fk:loadTranslationTable{
  ["emo__yuanjie"] = "渊劫",
  [":emo__yuanjie"] = "每当你使用【杀】指定目标后或成为【杀】的目标后，你可以展示目标和使用者共计至多两张手牌，若含【杀】，令展示的【杀】无次数限制；若含【闪】，此【杀】不可响应；若不含以上牌，你弃置所有展示牌。若仅含以上牌，此技能展示的牌上限+1。",

  ["#emo__yuanjie-invoke"] = "渊劫：你可以展示此【杀】使用者和目标角色共计至多 %arg 张手牌",
  ["#emo__yuanjie-card"] = "展示共计至多 %arg 张牌",
  ["@@emo__yuanjie-inhand"] = "渊劫",
  ["emo__yuanjie_poxi"] = "渊劫",
}

Fk:addPoxiMethod{
  name = "emo__yuanjie_poxi",
  card_filter = function (to_select, selected, data, extra_data)
    if not data or not extra_data then return false end
    return #selected < extra_data.n
  end,
  feasible = function(selected)
    return #selected > 0
  end,
  default_choice = function(data)
    local cards = data[1][2]
    return {cards[1]}
  end,
  prompt = function (data, extra_data)
    return "#emo__yuanjie-card:::" .. string.format("%.0f", extra_data.n)
  end,
}


---@param player ServerPlayer
---@param data AimData
local on_use = function (self, event, _, player, data)
  local room = player.room
  local to = (data.to == player) and data.from or data.to
  if player:isKongcheng() and to:isKongcheng() then return end
  local n = player:getMark("emo__yuanjie_extra") + 2
  local card_data, extra_data, visible_data = {}, {n = n}, {}
  if not player:isKongcheng() then
    table.insert(card_data, {player.general, player:getCardIds("h")})
  end
  if not to:isKongcheng() then
    table.insert(card_data, {to.general, to:getCardIds("h")})
    for _, id in ipairs(to:getCardIds("h")) do
      if not player:cardVisible(id) then
        visible_data[tostring(id)] = false
      end
    end
    if next(visible_data) == nil then visible_data = nil end
    extra_data.visible_data = visible_data
  end
  local chosen = room:askToPoxi(player, {
    poxi_type = "emo__yuanjie_poxi", cancelable = false, extra_data = extra_data, data = card_data,
  })
  local to_cards = table.filter(to:getCardIds("h"), function (id) return table.contains(chosen, id) end)
  local my_cards = table.filter(player:getCardIds("h"), function (id) return table.contains(chosen, id) end)
  if #to_cards > 0 then
    to:showCards(to_cards)
  end
  if #my_cards > 0 then
    player:showCards(my_cards)
  end
  local to_throw, my_throw = {}, {}
  for _, id in ipairs(chosen) do
    local card = Fk:getCardById(id)
    if card.trueName == "slash" then
      room:setCardMark(card, "@@emo__yuanjie-inhand", 1)
    elseif card.trueName == "jink" then
      data.disresponsive = true
      data.use.disresponsiveList = table.simpleClone(room.players)
    elseif table.contains(to_cards, id) then
      table.insert(to_throw, id)
    else
      table.insert(my_throw, id)
    end
  end
  local total = #to_throw + #my_throw
  if #chosen == total then
    to_throw = table.filter(to_throw, function (id) return table.contains(to:getCardIds("h"), id) end)
    if #to_throw > 0 then
      room:throwCard(to_throw, skel.name, to, player)
    end
    my_throw = table.filter(my_throw, function (id) return table.contains(player:getCardIds("h"), id) end)
    if #my_throw > 0 then
      room:throwCard(to_throw, skel.name, player, player)
    end
  elseif total == 0 then
    room:addPlayerMark(player, "emo__yuanjie_extra", 1)
  end
end

local can_trigger = function (self, event, target, player, data)
  return target == player and player:hasSkill(skel.name) and data.card.trueName == "slash" and
  data.from and not (data.to:isKongcheng() and data.from:isKongcheng()) and data.from ~= data.to
end

local on_cost = function (self, event, target, player, data)
  local n = player:getMark("emo__yuanjie_extra") + 2
  if player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__yuanjie-invoke:::"..n  }) then
    event:setCostData(self, { tos = {data.to} })
    return true
  end
end

skel:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.TargetConfirmed, {
  anim_type = "offensive",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect("targetmod", {
  bypass_times = function (self, player, skill, scope, card, to)
    return card and card.trueName == "slash" and card:getMark("@@emo__yuanjie-inhand") ~= 0
  end,
})

return skel
